/**
 * Copyright (c) 2020-2021 ThunderSoft
 * All Rights Reserved by Thunder Software Technology Co., Ltd and its affiliates.
 * You may not use, copy, distribute, modify, transmit in any form this file
 * except in compliance with ThunderSoft in writing by applicable law.
 *
 */
/**
 * @file    turbox_main.c
 * @brief   turbox app main process 
 * @details Initialize the system and create the threads
 * @version 1.0
 * @author  Hu lei
 * @date    2021-03-05
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                     NAME               DESCRIPTION
 * 2021-03-05               Hu lei              Create it.
 *
 */

/* STANDARD LIBC HEADERS */
#include <stdio.h>
#include <stdint.h>
#include <string.h>

/* ADAPTER LAYER HEADERS */
#include <FreeRTOS.h>
#include <aos/kernel.h>

/* ADAPTER LAYER HEADERS */
#include <bl_sys.h>
#include <bl_uart.h>
#include <bl_gpio.h>
#include <bl_flash.h>

#include <hal_sys.h>
#include <hal_gpio.h>
#include <hal_uart.h>

// #define TURBOX_FOR_CHECK_MEMORY
/*********************************************************************/
/*                            MACRO SECTION                          */
/*********************************************************************/
#define UART_ID_2                   ( 0x02 )
#define TASK_PRIORITY_FW            ( 0x1E )
#define CODE_CLI_BLSYNC_STOP        ( 0x02 )
#define CODE_CLI_BLSYNC_START       ( 0x01 )
#define mainHELLO_TASK_PRIORITY     ( 0x14 )
#define VALID_PC_START_XIP          ( 0x23000000 )
#define VALID_FP_START_XIP          ( 0x42000000 )

#define CLI_CMD_AUTOSTART1              "cmd_auto1"
#define CLI_CMD_AUTOSTART2              "cmd_auto2"
#define WIFI_AP_PSM_INFO_IP             "conf_ap_ip"
#define WIFI_AP_PSM_INFO_GW             "conf_ap_gw"
#define WIFI_AP_PSM_INFO_PMK            "conf_ap_pmk"
#define WIFI_AP_PSM_INFO_SSID           "conf_ap_ssid"
#define WIFI_AP_PSM_INFO_MASK           "conf_ap_mask"
#define WIFI_AP_PSM_INFO_DNS1           "conf_ap_dns1"
#define WIFI_AP_PSM_INFO_DNS2           "conf_ap_dns2"
#define WIFI_AP_PSM_INFO_BSSID          "conf_ap_bssid"
#define WIFI_AP_PSM_INFO_GW_MAC         "conf_ap_gw_mac"
#define WIFI_AP_PSM_INFO_CHANNEL        "conf_ap_channel"
#define WIFI_AP_PSM_INFO_PASSWORD       "conf_ap_psk"
#define WIFI_AP_PSM_INFO_IP_LEASE_TIME  "conf_ap_ip_lease_time"

#define TURBOX_PRINTF(...) do { \
    printf("[TURBOX_MAIN] Func %s, Line %d > ", __FUNCTION__, __LINE__); \
    printf(__VA_ARGS__); \
    printf("\r\n"); \
} while(0);

#define HAL_PARTITION_BL_ADDR 0x1FE000
#define FLASH_PAGE_SIZE 0x1000

//typedef struct
//{
//    uint8_t  net_type; // 0 softap    1 ble
//} bl_cfg_t;


/*********************************************************************/
/*                MACRO CHECK MEMORY                                 */
/*********************************************************************/
//#define TURBOX_FOR_CHECK_MEMORY

/*********************************************************************/
/*                declare and global define SECTION                  */
/*********************************************************************/
//static bl_cfg_t bl_cfg;


static void turbox_system_init(void)
{
/*    blog_init();
    bl_irq_init();
    bl_sec_init();
    bl_sec_test(); 
    bl_dma_init(); 
    hal_boot2_init();
    TURBOX_PRINTF("hal_boot2_init done....\r\n"); */

    /* board config is set after system is init*/
    /*hal_board_cfg(0);
    TURBOX_PRINTF("hal_board_cfg done....\r\n");*/
}

static void bl602_gpio_init()
{
    uint8_t gpio_ready=4;
    bl_gpio_enable_output(gpio_ready,0,0);
    bl_gpio_output_set(gpio_ready, 1);
    //bl_gpio_enable_input(gpio_reload, 0, 0);
}

/* void bl_gpio_set(uint8_t value)
{
    uint8_t gpio_ready=4;
    if(value)
    {
        bl_gpio_enable_output(gpio_ready,0,0);
        bl_gpio_output_set(gpio_ready, 1);
    }
    else
    {
        bl_gpio_enable_output(gpio_ready,0,0);
        bl_gpio_output_set(gpio_ready, 0);
    }
} */

void bfl_main()
{
    UINT32 ret = 0;
    bl_sys_early_init();
    bl_uart_init(0, 16, 7, 255, 255, 921600);
    
    TURBOX_PRINTF("Welcome to Turbox Module.\r\n");
    bl_sys_init();
    
    /* Init Kernel */
    ret = LOS_KernelInit();
    if (ret != LOS_OK) {
        TURBOX_PRINTF("kernel init error\n");
        while(1);
    } 
    //RunTaskSample();
    printf("go on\r\n");
    OHOS_SystemInit();  
    if (DeviceManagerStart()) {
        printf("[%s] No drivers need load by hdf manager!",__func__);
    }else {
        printf("hdf success!\n");
    }

    turbox_system_init();
    TURBOX_PRINTF("Starting turbox_loop_proc task...\r\n");
    bl602_gpio_init();
    TURBOX_PRINTF("Starting OS Scheduler...\r\n");
    LOS_Start();
}
